package com.yhb.map;

import java.util.ArrayList;
import java.util.HashMap;

import com.yhb.map.log.Log;

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class RouteListActivity extends Activity {
	
	private ExpandableListView mList;
	private ArrayList<HashMap<String, Object>>mResultList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.route_list_activity);
		
		Intent intent = getIntent();
		RouteResultParcelable resultParcelable = intent.getParcelableExtra("result");
		mResultList = resultParcelable.getResult();
		mList = (ExpandableListView) findViewById(R.id.list);
		mList.setAdapter(new ResultListAdapter(this));
	}
	
	private class ResultListAdapter extends BaseExpandableListAdapter {
		
		private TextView pStartText;
		private TextView pEndText;
		private TextView pDistanceText;
		private TextView pDurationText;
		private TextView cPathText;
		private TextView cDistanceText;
		private TextView cDurationText;
			
		private Context context;
		
		public ResultListAdapter(Context context) {
			this.context = context;		
		}
		
		@Override
		public HashMap<String, String> getChild(int groupPosition, int childPosition) {
			Log.d("getChild()");
			ArrayList<HashMap<String, String>> childList 
			        = (ArrayList<HashMap<String, String>>) getGroup(groupPosition).get("lines");
			return childList.get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			Log.d("getChildId()");
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			Log.d("getChildView()");
			ArrayList<HashMap<String, String>> childList 
	                = (ArrayList<HashMap<String, String>>) getGroup(groupPosition).get("lines");
			HashMap<String, String> childData = childList.get(childPosition);
			convertView = updateChildViews(convertView, childData);
			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			Log.d("getChildrenCount()");
			ArrayList<HashMap<String, String>> childList 
	        = (ArrayList<HashMap<String, String>>) getGroup(groupPosition).get("lines");
			return childList.size();
		}

		@Override
		public HashMap<String, Object> getGroup(int groupPosition) {	
			Log.d("getGroup()");
			return mResultList.get(groupPosition);
		}

		@Override
		public int getGroupCount() {
			Log.d("getGroupCount()");
			return mResultList.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			Log.d("getGroupId()");
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			Log.d("getGroupView()");
			HashMap<String, Object> groupData = getGroup(groupPosition);
			convertView = updateGroupViews(convertView, groupData);
			Log.d("convertView = " + convertView);
			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return false;
		}
		
		private View updateGroupViews(View view, HashMap<String, Object> data) {
			
			LayoutInflater inflater = LayoutInflater.from(context);
			view = inflater.inflate(R.layout.route_result_item, null);
			initGroupViews(view);
			Log.d("view = " + view);
			
			String start = (String) data.get("start_Address");
			String end = (String) data.get("end_address");
			String distance = (String) data.get("distance");
			String duration = (String) data.get("duration");
			pStartText.setText(start);
			pEndText.setText(end);
			pDistanceText.setText(distance);
			pDurationText.setText(duration);
			return view;
		}
		
		private View updateChildViews(View view, HashMap<String, String> data) {
			LayoutInflater inflater = LayoutInflater.from(context);
			view = inflater.inflate(R.layout.route_result_child_item, null);
			initChildViews(view);
			
			String path = (String) data.get("stepText");
			String distance = (String) data.get("distance");
			String duration = (String) data.get("duration");
			cPathText.setText(path);
			cDistanceText.setText(distance);
			cDurationText.setText(duration);
			return view;
		}
		
		private void initGroupViews(View view) {
			pStartText = (TextView)view.findViewById(R.id.route_start_address);
			pEndText = (TextView)view.findViewById(R.id.route_end_address);
			pDistanceText = (TextView)view.findViewById(R.id.route_distance);
			pDurationText = (TextView)view.findViewById(R.id.route_duration);
		}
		
		private void initChildViews(View view) {
			cPathText = (TextView)view.findViewById(R.id.route_sub_line);
			cDistanceText = (TextView)view.findViewById(R.id.route_sub_distance);
			cDurationText = (TextView)view.findViewById(R.id.route_sub_duration);
		}
		
	}
}
